﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace RaidAnything.FS
{
    /// <summary>
    /// represents a uniquely identifiable RS Byte Array
    /// </summary>
    public class Block
    {
        /// <summary>
        /// constructs a block with the specified contents
        /// </summary>
        /// <param name="contents"></param>
        /// <param name="contentLength"></param>
        public Block(byte[] contents, int contentLength)
        {
            Contract.Requires(contents != null);
            Contract.Requires(contentLength > 1, "Contents must be at least one byte");
            Contract.Requires(contents.Length <= contentLength, "Contents exceeds buffer size");

            _contentLength = contentLength;
            _data = new byte[contentLength];
            //Contract.Assume(contentLength <= contents.GetLowerBound(0) + contents.Length);
            //Contract.Assume(contentLength <= _data.GetLowerBound(0) + _data.Length); 
            ArrayCopy(contents, _data, contentLength);
        }

        [ContractVerification(false)]
        private static void ArrayCopy(Array source, Array dest, int length)
        {
            Array.Copy(source, dest, length);
        }

        private byte[] _data;
        private int _contentLength;

        /// <summary>
        /// the decoded block data
        /// </summary>
        public byte[] Data
        {
            get 
            {
                return _data;
            }
        }
        
        /// <summary>
        /// The length of the content in the block
        /// </summary>
        public int ContentLength { get { return _contentLength; } }
    }
}
